/*
 * Copyright (c) 2011 Google Inc.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */

package com.snj.visangedu.calendar;


import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import android.os.AsyncTask;
import android.os.Handler;
import android.util.Log;

import com.google.api.client.http.HttpHeaders;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.util.DateTime;
import com.snj.visangedu.studytime.StudyPlannerFragment;

/**
 * @author AA@google.com (Your Name Here)
 *
 */
public class GC_Update_Event extends AsyncTask<Void, Void, Void> {

  private static final String TAG = "StudyPlan";

  private boolean bReturn = true;

  GC_Update_Event() {
  }

  @Override
  protected void onPreExecute() {
  }

  @Override
  protected Void doInBackground(Void... arg0) {

    try {

      AsyncManager.Instance.events.clear();

      com.google.api.services.calendar.model.Events events =
    		  StudyPlannerFragment.Instance.client.events().list(StudyPlannerFragment.Instance.workCalendarInfo.id)
              .execute();

      List<com.google.api.services.calendar.model.Event> item = events.getItems();

      for (int i = 0; i < item.size(); i++) {
        com.google.api.services.calendar.model.Event evt = item.get(i);

        CalendarEventInfo info = new CalendarEventInfo(evt.getId(), evt.getSummary());

        // Log.d("StudyPlan", "Event summary = " + evt.getSummary());

        info.szColorId = evt.getColorId();
        info.szDescription = evt.getDescription();
        info.szEtag = evt.getEtag();
        info.szKind = evt.getKind();
        info.szLocation = evt.getLocation();

        // EventReminder getResponseHeaders
        HttpHeaders header = evt.getResponseHeaders();
        if (header != null)
          info.szResponseHeaders = header.toString();
        else
          info.szResponseHeaders = "";

        JsonFactory jf = evt.getFactory();
        if (jf != null)
          info.szFactory = jf.toString();
        else
          info.szFactory = "";
        com.google.api.services.calendar.model.EventDateTime sdate = evt.getStart();
        com.google.api.services.calendar.model.EventDateTime edate = evt.getEnd();

        // info.sdt = sdate.getDate();
        DateTime dt = sdate.getDateTime();

        if (dt == null) {
          info.sdt = sdate.getDate();
          info.sdt += " 00:00";
        } else {
          long utcTime = dt.getValue();
          Date date = new Date(utcTime);

          SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.KOREA);
          info.sdt = formatter.format(date);
        }

        dt = edate.getDateTime();

        if (dt == null) {
          info.edt = edate.getDate();
          info.edt += " 00:00";
        } else {
          long utcTime = dt.getValue();
          Date date = new Date(utcTime);
          SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.KOREA);
          info.edt = formatter.format(date);
        }

        info.AutoSetTimeStamp();

        AsyncManager.Instance.events.add(info);
      }
    } catch (IOException e) {
      Log.d(TAG, "IOException =" + e.getMessage());
      bReturn = false;
    } catch (Exception e) {
      Log.d(TAG, "> getEvent Exception =" + e.getMessage());
      bReturn = false;
    } finally {
    }
    return null;
  }

  @Override
  protected void onPostExecute(Void result) {
	  
	try
	{
		AsyncManager.Instance.WorkProcessComplete("update event", bReturn);
	}
	catch(Exception e)
	{
		e.printStackTrace();
	}
	
  }
}
